package com.zzyl.task;

import cn.hutool.core.date.DateUtil;
import com.zzyl.entity.CustomerReservation;
import com.zzyl.mapper.CustomerReservationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 定时任务
 */

@Slf4j
@Component
public class ReservationTask {

    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    /**
     * 定时更新预约状态
     */
    @Scheduled(cron ="0 0/30 8-18 * * ? " )          //"0/5 * * * * ?"
    public void updateReservationTaskStatus(){
        /* 每日23:00更新：若订单状态为待上门 && 预约时间段已过，则修改状态为已过期 */
        log.info("预约状态自动更新 执行了一次");

        //1、查询所有预约信息，返回id、status、time
        List<CustomerReservation> reservations = customerReservationMapper.selectAll();

        //2、遍历所有预约信息，筛选满足条件的
        for (CustomerReservation reservation : reservations) {
            //拿到预约状态
            Integer status = reservation.getStatus();
            //拿到预约时间，转为LocalDateTime格式
            String theTime = reservation.getTime();
            LocalDateTime time = DateUtil.parseLocalDateTime(theTime, "yyyy-MM-dd HH:mm:ss");
            //判断
            if (status == 0 && LocalDateTime.now().isAfter(time)) {
                //更新
                customerReservationMapper.autoUpdate(reservation.getId());
            }
        }
    }









}
